package com.sc.nft.dao;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.EcologyPoolCommunityServiceRank;
import com.sc.nft.enums.EcologyPoolCommunityServiceRankTypeEnum;
import com.sc.nft.mapper.EcologyPoolCommunityServiceRankMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author 刘国航
 * @since 2024/7/16
 */
@Slf4j
@Repository
public class EcologyPoolCommunityServiceRankDao extends ServiceImpl<EcologyPoolCommunityServiceRankMapper, EcologyPoolCommunityServiceRank> {

    public EcologyPoolCommunityServiceRank getMaxRankDate(Long confId) {
        LambdaQueryWrapper<EcologyPoolCommunityServiceRank> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(EcologyPoolCommunityServiceRank::getEcologyPoolCommunityServiceRankConfId, confId);
        queryWrapper.orderByDesc(EcologyPoolCommunityServiceRank::getRankDate);
        queryWrapper.last("limit 1");
        return baseMapper.selectOne(queryWrapper);
    }

    public Page<EcologyPoolCommunityServiceRank> showPage(Page<EcologyPoolCommunityServiceRank> page, Long rankConfId, Date rankDate) {
        LambdaQueryWrapper<EcologyPoolCommunityServiceRank> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoolCommunityServiceRank::getEcologyPoolCommunityServiceRankConfId, rankConfId);
        wrapper.eq(EcologyPoolCommunityServiceRank::getRankDate, rankDate);
        wrapper.eq(EcologyPoolCommunityServiceRank::getShowStatus, Boolean.TRUE);
        wrapper.orderByAsc(EcologyPoolCommunityServiceRank::getSort);
        return page(page, wrapper);
    }

    public EcologyPoolCommunityServiceRank getOneByDateAndUser(Long rankConfId, Date rankDate, Long daoId, Long userId) {
        LambdaQueryWrapper<EcologyPoolCommunityServiceRank> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoolCommunityServiceRank::getEcologyPoolCommunityServiceRankConfId, rankConfId);
        wrapper.eq(EcologyPoolCommunityServiceRank::getRankDate, rankDate);
        wrapper.eq(EcologyPoolCommunityServiceRank::getDaoId, daoId);
        wrapper.eq(EcologyPoolCommunityServiceRank::getUserId, userId);
        wrapper.orderByDesc(EcologyPoolCommunityServiceRank::getId);
        wrapper.last("limit 1");
        return baseMapper.selectOne(wrapper);
    }

    public void saveBatchExchequer(List<EcologyPoolCommunityServiceRank> list) {
        saveBatch(list);
    }

    public EcologyPoolCommunityServiceRank listByRankConfIdAndcContributionThreshold(Long configId, BigDecimal newEcologySmeltingThreshold, Integer newUserCountThreshold, BigDecimal configNewEcologySmeltingThreshold, Integer configNewUserCountThreshold, List<Long> userIdExcludeList) {
        LambdaQueryWrapper<EcologyPoolCommunityServiceRank> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EcologyPoolCommunityServiceRank::getEcologyPoolCommunityServiceRankConfId, configId);
        wrapper.ge(EcologyPoolCommunityServiceRank::getNewEcologySmelting, newEcologySmeltingThreshold);
        wrapper.ge(EcologyPoolCommunityServiceRank::getNewUserCount, newUserCountThreshold);
        wrapper.ge(EcologyPoolCommunityServiceRank::getNewEcologySmelting, configNewEcologySmeltingThreshold);
        wrapper.ge(EcologyPoolCommunityServiceRank::getNewUserCount, configNewUserCountThreshold);
        wrapper.eq(EcologyPoolCommunityServiceRank::getShowStatus, Boolean.FALSE);
        wrapper.notIn(CollUtil.isNotEmpty(userIdExcludeList), EcologyPoolCommunityServiceRank::getUserId, userIdExcludeList);
        wrapper.orderByDesc(EcologyPoolCommunityServiceRank::getNewEcologySmelting);
        wrapper.orderByDesc(EcologyPoolCommunityServiceRank::getNewUserCount);
        wrapper.orderByAsc(EcologyPoolCommunityServiceRank::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public EcologyPoolCommunityServiceRank compareDaoRank(Long userId, DateTime dateTime, EcologyPoolCommunityServiceRankTypeEnum rankType) {
        LambdaQueryWrapper<EcologyPoolCommunityServiceRank> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EcologyPoolCommunityServiceRank::getUserId, userId);
        // 把时间dateTime转换格式为YYYY-MM-DD
        wrapper.eq(EcologyPoolCommunityServiceRank::getRankDate, dateTime.toString("yyyy-MM-dd"));
        wrapper.eq(EcologyPoolCommunityServiceRank::getRankType, rankType);
        wrapper.eq(EcologyPoolCommunityServiceRank::getShowStatus, Boolean.TRUE);
        wrapper.orderByDesc(EcologyPoolCommunityServiceRank::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public List<EcologyPoolCommunityServiceRank> waitAutoSettleList(Long rankConfId, Date rankDate) {
        LambdaQueryWrapper<EcologyPoolCommunityServiceRank> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoolCommunityServiceRank::getEcologyPoolCommunityServiceRankConfId, rankConfId);
        wrapper.eq(EcologyPoolCommunityServiceRank::getRankDate, rankDate);
        wrapper.eq(EcologyPoolCommunityServiceRank::getShowStatus, Boolean.TRUE);
        wrapper.eq(EcologyPoolCommunityServiceRank::getHasSort, Boolean.TRUE);
        wrapper.orderByAsc(EcologyPoolCommunityServiceRank::getSort);
        return baseMapper.selectList(wrapper);
    }

}
